package com.glasses.spectrum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.MenuModel;

/**
 *
 * @author Jean-Luc Burot
 * @since 2014-12-13
 * @version 1
 */
@Named
@RequestScoped
public class TopMenuBean implements Serializable {
    private MenuModel model;
    
    @Inject
    private GetParam getParam;
    
    private List<ViewItem> views;
    
    public MenuModel getModel() {
        if (this.model == null) {
            initMenu();
        }
        
        return model;
    }
    
    public void setModel(MenuModel model) {
        this.model = model;
    }
    
    public Integer getActiveIndex() {
        return getViewIndex(this.getParam.getView());
    }
    
    private void initMenu() {
        this.model = new DefaultMenuModel();
        this.views = new ArrayList<>();
        
        //Define Top Menu
        this.views.add(new ViewItem("Home", "home", true));
        this.views.add(new ViewItem("Tour", "tour", false));
        this.views.add(new ViewItem("Impressum", "impressum", false));
        this.views.add(new ViewItem("Members", "members", false));
        
        //Build Top Menu
        for(ViewItem viewItem : this.views) {
            DefaultMenuItem menuItem = new DefaultMenuItem();
            menuItem.setOutcome("index");
            menuItem.setValue(viewItem.getViewName());
            
            //Set language param
            //menuItem.setParam("language", this.indexBean.getLanguage());
            menuItem.setParam("language", this.getParam.getLanguage());
            
            //Set view param
            if (viewItem.getDefaultItem() == false) {
                menuItem.setParam("view", viewItem.getViewAlias());
            }
            
            this.model.addElement(menuItem);
        }
    }
    
    //Gets the index of the view array. Returns 0 if not found or null.
    private Integer getViewIndex(String view) {
        Integer pos = 0;
        
        if (view != null) {
            for(int i=0; i<this.views.size(); i++) {
                if (view.equals(this.views.get(i).getViewAlias())) {
                    pos = i;
                    break;
                }
            }
        }
        
        return pos;
    }
    
    
    public void BrowserLocale() {
        Locale browserLocaleA = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        System.out.println("browserLocaleA: ");
        System.out.println("browserLocaleA.getCountry(): " + browserLocaleA.getCountry());
        System.out.println("browserLocaleA.getDisplayCountry(): " + browserLocaleA.getDisplayCountry());
        System.out.println("browserLocaleA.getDisplayLanguage(): " + browserLocaleA.getDisplayLanguage());
        System.out.println("browserLocaleA.getDisplayName(): " + browserLocaleA.getDisplayName());
        System.out.println("browserLocaleA.getDisplayScript(): " + browserLocaleA.getDisplayScript());
        System.out.println("browserLocaleA.getDisplayVariant(): " + browserLocaleA.getDisplayVariant());
        System.out.println("browserLocaleA.getISO3Country(): " + browserLocaleA.getISO3Country());
        System.out.println("browserLocaleA.getISO3Language(): " + browserLocaleA.getISO3Language());
        System.out.println("browserLocaleA.getLanguage(): " + browserLocaleA.getLanguage());
        System.out.println("browserLocaleA.getScript(): " + browserLocaleA.getScript());
        System.out.println("browserLocaleA.getVariant(): " + browserLocaleA.getVariant());
        System.out.println("browserLocaleA.toLanguageTag(): " + browserLocaleA.toLanguageTag());
        System.out.println("browserLocaleA.toString(): " + browserLocaleA.toString());
        System.out.println("FacesContext.getCurrentInstance().getViewRoot().getLocale(): " + browserLocaleA);
        
        Locale browserLocaleB = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
        System.out.println("browserLocaleB:");
        System.out.println("browserLocaleB.getCountry(): " + browserLocaleB.getCountry());
        System.out.println("browserLocaleB.getDisplayCountry(): " + browserLocaleB.getDisplayCountry());
        System.out.println("browserLocaleB.getDisplayLanguage(): " + browserLocaleB.getDisplayLanguage());
        System.out.println("browserLocaleB.getDisplayName(): " + browserLocaleB.getDisplayName());
        System.out.println("browserLocaleB.getDisplayScript(): " + browserLocaleB.getDisplayScript());
        System.out.println("browserLocaleB.getDisplayVariant(): " + browserLocaleB.getDisplayVariant());
        System.out.println("browserLocaleB.getISO3Country(): " + browserLocaleB.getISO3Country());
        System.out.println("browserLocaleB.getISO3Language(): " + browserLocaleB.getISO3Language());
        System.out.println("browserLocaleB.getLanguage(): " + browserLocaleB.getLanguage());
        System.out.println("browserLocaleB.getScript(): " + browserLocaleB.getScript());
        System.out.println("browserLocaleB.getVariant(): " + browserLocaleB.getVariant());
        System.out.println("browserLocaleB.toLanguageTag(): " + browserLocaleB.toLanguageTag());
        System.out.println("browserLocaleB.toString(): " + browserLocaleB.toString());
        System.out.println("FacesContext.getCurrentInstance().getExternalContext().getRequestLocale(): " + browserLocaleB);
    }
    
    public void method() {
        System.out.println("INVOKED METHOD TopMenuBean.method()");
    }
    
    public String getLalala() {
        return "lalalalala: ";
    }
}
